package com.bawei.park2301.notice

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bawei.libs_base.entity.NoticeBeanItem
import com.bawei.libs_base.net.ApiService

class NoticeDataSource(apiService: ApiService): PagingSource<Int, NoticeBeanItem>() {

    var api=apiService

    override fun getRefreshKey(state: PagingState<Int, NoticeBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeBeanItem> {
        return try {
            val currenPage= params.key?:0
            val data=api.getNoticeList(currenPage,10)
            val nextPage = if (data.data.size == 10) currenPage + 1 else null
            return LoadResult.Page(data.data,null,nextPage)
        }catch (e: Exception){
            LoadResult.Error(e)
        }
    }
}